﻿using System;
using System.Net;
using ThePresenter.Net.PresenterCom.Uids;

namespace ThePresenter.Net.NetCom
{
	[Serializable]
	public class ClientInfo
	{
		#region Fields
		Uid _Uid;

		String _Name;
		
		IPEndPoint _ClientEndPoint;
	
		Boolean _HasLogicalInfo;
		
		Boolean _HasPhysicalInfo; 
		#endregion


		#region Properties
		/// <summary>
		/// Gets the UID.
		/// </summary>
		/// <value>The UID.</value>
		public Uid Uid
		{
			get { return _Uid; }
		}

		/// <summary>
		/// Gets the name.
		/// </summary>
		/// <value>The name.</value>
		public String Name
		{
			get { return _Name; }
		}

		/// <summary>
		/// Gets the client end point.
		/// </summary>
		/// <value>The client end point.</value>
		public IPEndPoint ClientEndPoint
		{
			get { return _ClientEndPoint; }
		}

		/// <summary>
		/// Gets the IP.
		/// </summary>
		/// <value>The IP.</value>
		public String IP
		{
			get
			{
				if (_HasPhysicalInfo)
					return _ClientEndPoint.Address.ToString();
				else
					return "No IP information";
			}
		}

		/// <summary>
		/// Gets a value indicating whether this instance is complete.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is complete; otherwise, <c>false</c>.
		/// </value>
		public bool IsComplete
		{
			get { return _HasLogicalInfo && _HasPhysicalInfo; }
		}
		#endregion


		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="ClientInfo"/> class.
		/// </summary>
		/// <remarks>Used by server, when connection is incoming. Should be updated later by server when Advertisement action is received using FillLogicalInfo method.</remarks>
		/// <param name="S">The Socket.</param>
		public ClientInfo(IPEndPoint RemoteEndPoint)
		{
			_ClientEndPoint = RemoteEndPoint;

			_HasPhysicalInfo = true;
			_HasLogicalInfo = false;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ClientInfo"/> class.
		/// </summary>
		/// <remarks>Used by client as informations for server.</remarks>
		/// <param name="uid">The UID.</param>
		/// <param name="name">The name.</param>
		public ClientInfo(Uid uid, String name)
		{
			_Uid = uid;
			_Name = name;

			_HasPhysicalInfo = false;
			_HasLogicalInfo = true;
		}
		#endregion


		#region Methods
		/// <summary>
		/// Fills the logical info about client.
		/// </summary>
		/// <remarks>Used by server to fill in missing informations about client.</remarks>
		/// <param name="uid">The UID.</param>
		/// <param name="name">The name.</param>
		public void FillLogicalInfo(Uid uid, String name)
		{
			_Uid = uid;
			_Name = name;

			_HasLogicalInfo = true;
		}

		/// <summary>
		/// Returns a <see cref="System.String"/> that represents this instance.
		/// </summary>
		/// <returns>
		/// A <see cref="System.String"/> that represents this instance.
		/// </returns>
		public override string ToString()
		{
			if (this.IsComplete)
				return String.Format("Username: {0}\nIP: {1}\nUID: {2}", _Name, this._ClientEndPoint.Address.ToString(), _Uid);
			else
			{
				if (this._HasLogicalInfo)
					return String.Format("Username: {0}\nUID: {1}", _Name, _Uid);
				if (this._HasPhysicalInfo)
					return String.Format("IP: {0}", this._ClientEndPoint.Address.ToString());
				else
					return "Unknown client";
			}

		}	
		#endregion
	}
}
